package cn.iocoder.yudao.module.exam.service.exam;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.exam.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.exam.ExamDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.exam.ExamMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.EXAM_NOT_EXISTS;

/**
 * 考试管理 Service 实现类
 *
 * @author Francis
 */
@Service
@Validated
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamMapper examMapper;

    @Override
    public Long createExam(ExamSaveReqVO createReqVO) {
        // 插入
        ExamDO exam = BeanUtils.toBean(createReqVO, ExamDO.class);
        examMapper.insert(exam);

        // 返回
        return exam.getId();
    }

    @Override
    public void updateExam(ExamSaveReqVO updateReqVO) {
        // 校验存在
        validateExamExists(updateReqVO.getId());
        // 更新
        ExamDO updateObj = BeanUtils.toBean(updateReqVO, ExamDO.class);
        examMapper.updateById(updateObj);
    }

    @Override
    public void deleteExam(Long id) {
        // 校验存在
        validateExamExists(id);
        // 删除
        examMapper.deleteById(id);
    }

    @Override
        public void deleteExamListByIds(List<Long> ids) {
        // 删除
        examMapper.deleteByIds(ids);
        }


    private void validateExamExists(Long id) {
        if (examMapper.selectById(id) == null) {
            throw exception(EXAM_NOT_EXISTS);
        }
    }

    @Override
    public ExamDO getExam(Long id) {
        return examMapper.selectById(id);
    }

    @Override
    public PageResult<ExamDO> getExamPage(ExamPageReqVO pageReqVO) {
        return examMapper.selectPage(pageReqVO);
    }

}